package com.company.graphic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FindCenterOfGraphic {

    public int findCenter(int[][] edges) {
        int[] nodes = new int[edges.length*2];

        Map<Integer,Integer> nodeConnectTimes = new HashMap<>();
        //统计所以节点
        for (int i = 0; i < edges.length; i++) {
            int n1 = edges[i][0];
            int n2 = edges[i][1];

            if (nodeConnectTimes.containsKey(n1)) {
                Integer time = nodeConnectTimes.get(n1);
                nodeConnectTimes.put(n1,time+1);
            }else {
                nodeConnectTimes.put(n1,1);
            }

            if (nodeConnectTimes.containsKey(n2)) {
                Integer time = nodeConnectTimes.get(n2);
                nodeConnectTimes.put(n2,time+1);
            }else {
                nodeConnectTimes.put(n2,1);
            }
        }
//        //统计每个节点相连的节点
//        Map<Integer,List<Integer>> relationMap=new HashMap<>();
//        for (int node : nodes) {
//            List<Integer> connectedNodes = new ArrayList<>();
//            for (int[] edge : edges) {
//                if (edge[0]==node) {
//                    connectedNodes.add(edge[1]);
//                }
//                if (edge[1]==node){
//                    connectedNodes.add(edge[0]);
//                }
//            }
//            relationMap.put(node,connectedNodes);
//            System.out.println(connectedNodes);
//        }
//        System.out.println(relationMap);
        int maxConnectedNode =0;
        int nodeCount=0;
        for (Integer node : nodeConnectTimes.keySet()) {
            int times = nodeConnectTimes.get(node);
            if (times>nodeCount) {
                maxConnectedNode = node;
                nodeCount = times;
            }
        }
        return maxConnectedNode;
    }
}
